package com.video.netty.InChannelHandler;

import com.alibaba.fastjson.JSONObject;
import com.video.commons.task.TaskStatusUtil;
import com.video.service.task.ITaskService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * 刷新终端状态
 *
 * 终端状态刷新 0-任务空闲  1-执行中 2-停止 3-暂停
 *
 * {"id":"refreshTerminalState","account":"admin", "taskid":"145",
 * "status":"1", "mp3":"1.mp3",  "duration": 90, "startDate":"2018-12-14",
 * "users":["1", "2", "3"], "curtime":"160", "msgid": "xxxxxxxxxxxxx"}
 *
 *
 * {"id":"refreshTerminalState","account":"admin","taskid":"c62db609-3b76-4814-a6c6-bae5739c38e5",
 * "status":1,"mp3":"蔡健雅 - 你的温度.mp3","users":["6"],"duration":219,"startDate":""}
 *
 *
 * {"id":"refreshTerminalState","account":"huangxinghua",
 * "taskid":"af16925e-85dd-44d9-82b5-863c0fa1947e","status":1,
 * "mp3":"磨蹭大王 - 冻结 (Cover_ 林俊杰).mp3","users":["801","802"],
 * "duration":285,"startDate":"2020-03-28","curtime":4283}
 *
 * {"id":"refreshTerminalState","account":"huangcdian","taskid":"1a126e28-a0ee-46f5-8466-62bec9b7ba2e",
 * "status":1,"mp3":"刘珂矣 - 半壶纱.mp3",
 * "users":["803","203","219","204","974","205","211"],"duration":225,
 * "startDate":"2020-03-23","curtime":433350}
 *
 * 问题：
 * {"duration":222,"mp3":"[邓丽君]小村之恋-邓丽君.mp3",
 * "curtime":0,"id":"refreshTerminalState","account":"zmgb",
 * "taskid":"03ed85f1-bd15-4d22-b4e3-0a6cb1379336","users":["118","600"],
 * "startDate":"2020-05-25","status":1}
 *
 */
@BaseChannelHandler.MsgHandler
public class WsRefreshTerminalStateChannelHandler extends BaseChannelHandler {

    @Autowired
    private ITaskService taskService;

    @Autowired
    private TaskStatusUtil taskStatusUtil;

    @Override
    protected String msgId() {
        return "refreshTerminalState";
    }

    /**
     *
     * 终端状态刷新 0-任务空闲  1-执行中 2-停止 3-暂停
     *
     * @param json
     * @throws Exception
     */
    @Override
    protected void msgHandler(JSONObject json) throws Exception {

        String taskid = json.getString("taskid");//任务id
        Integer status = json.getInteger("status");//当前状态  0-任务空闲 1-执行中 2-停止 3-暂停
        String mp3 = json.getString("mp3");//当前曲目
        Integer duration = json.getInteger("duration");//持续时间
        Integer curtime = json.getInteger("curtime");//当前进度
        String startDate = json.getString("startDate");//开始日期
        String[] users = json.getJSONArray("users").toArray(new String[0]);//终端列表

        //更新redis的任务状态
        taskStatusUtil.updateTaskStatus(taskid, status, mp3, duration, startDate, users);

        //更新数据库
        taskService.updateTaskStatus(taskid, status, startDate);
    }
}
